// ==UserScript==
// @name        上下键翻页、左键下一页（适用于台式机）
// @namespace   Violentmonkey Scripts
// @match       http://m.biquge5200.net/*
// @match       https://www.qidian.com/chapter/*
// @match       https://www.beqege.cc/*/*
// @match       https://www.22biqu.com/*/*
// @match       https://www.kuaishu5.com/*/*
// @match       https://www.shuzhaige.com/*/*
// @grant       none
// @version     1.1
// @author      Goocheez
// @description 给一些常看的小说网站，增加键盘上下键翻页功能，以及左键下一页
// @grant        GM_addStyle
// ==/UserScript==
(()=>{
//--------------------------------------------------------------------------------------
//                           总引导
//--------------------------------------------------------------------------------------
  const actions = {
    'm.biquge5200.net': ()=>{biquge5200()},
    'www.qidian.com':   ()=>{ qidian() },
    'www.beqege.cc':    ()=>{ beqege() },
    'www.22biqu.com':   ()=>{ biqu() },
    'www.kuaishu5.com': ()=>{ kuaishu5() },
    'www.shuzhaige.com':()=>{ shuzhaige() },
  }

  if ( actions[window.location.hostname] ){
    actions[window.location.hostname]();
  }


//--------------------------------------------------------------------------------------
//                                 通用函数
//--------------------------------------------------------------------------------------

function scrollMe(line, direction = "down") {
    const scrollAmount = window.innerHeight * 0.8 * (direction === "down" ? 1 : -1);
    const mark_class = 'gm-scroll-mark';
    document.querySelectorAll('.' + mark_class).forEach(l=> l.remove()); // 删除旧线

    // 获取当前视口底部位置
    const viewportBottom = window.scrollY + window.innerHeight - 10;

    // 创建标记线，置于当前阅读页面的底部
    const mark = document.createElement('div');
    mark.className = mark_class;
    Object.assign(mark.style, {
      position: 'absolute',
      left: 0,
      width: '100%',
      height: '2px',
      //background: '#969c65f2',
      backgroundImage: 'repeating-linear-gradient(to right, #969c6590 0 8px, transparent 8px 16px)',
      zIndex: '9998',          // 比九宫格略低即可
      pointerEvents: 'none',
      top: viewportBottom + 'px'
    });
    document.body.appendChild(mark);

    // 带着标记线，执行滚动
    window.scrollBy({
      top: scrollAmount,
      behavior: 'smooth'
    });
  }



//--------------------------------------------------------------------------------------
//                        不同网站的处理函数
//--------------------------------------------------------------------------------------
function biquge5200(){
  prev = document.getElementsByClassName('navigator-nobutton')[0].childNodes[1].childNodes[0].firstChild
  next = document.getElementsByClassName('navigator-nobutton')[0].childNodes[1].childNodes[6].firstChild
  document.addEventListener('keydown', (e)=>{
    if(e.key == 'ArrowLeft'){
      prev.click()
    }else if(e.key == 'ArrowRight'){
      //console.log('rr')
      next.click()
    }else if(e.key == 'ArrowDown'){
      scrollMe("p")
        console.log('key down')
    }else if(e.key == 'ArrowUp'){
      console.log('key up')
    }
  })
}

function qidian(){
  /*document.querySelector('#reader').addEventListener('click', () => {
      scallMe();
  });*/
  document.addEventListener('keydown', (e)=>{
    if(e.key == 'ArrowDown'){
      scrollMe("p > span.content-text");
    }else if(e.key == 'ArrowUp'){
      scrollMe("p > span.content-text", "up");
    }
  })
}

  function beqege(){
    document.addEventListener('keydown', (e)=>{
      if(e.key == 'ArrowDown'){
        scrollMe("#content p");
      }else if(e.key == 'ArrowUp'){
        scrollMe("#content p", "up");
      }
    })
  }

  function biqu(){
    document.addEventListener('keydown', (e)=>{
      if(e.key == 'ArrowDown'){
        scrollMe("#content p");
      }else if(e.key == 'ArrowUp'){
        scrollMe("#content p", "up");
      }
    })
  }

  function kuaishu5(){
    document.addEventListener('load', function() {
      console.log("loaded,changing ...")
      document.body.style.backgroundColor = '#333'; // 深灰色
    });
    document.addEventListener('keydown', (e)=>{
      if(e.key == 'ArrowDown'){
        scrollMe("#content p");
      }else if(e.key == 'ArrowUp'){
        scrollMe("#content p", "up");
      }else if(e.key == 'ArrowRight'){
        document.querySelector('a[rel="next"]').click();
      }
    })
  }

  function shuzhaige(){
    if (navigator.userAgent.includes('Firefox')) {
        document.querySelector('#content').style.MozUserSelect='none';
    }
    document.addEventListener('keydown', e=>{
      if(e.key === 'ArrowDown'){
          scrollMe('#content p')
      }else if(e.key === 'ArrowUp'){
        scrollMe('#content p', "up")
      }else if(e.key === 'ArrowRight'){
        document.querySelector('.btn.btn-danger.pull-right.col-md-2.col-xs-3.col-sm-3').click()
      }

    })
  }

})()